package com.itheima.service.impl;

import com.itheima.dao.CommentDao;
import com.itheima.dao.CommentReportDao;
import com.itheima.pojo.Comment;
import com.itheima.pojo.CommentReport;
import com.itheima.pojo.User;
import com.itheima.service.CommentService;
import com.itheima.utils.MessageConstant;
import com.itheima.vo.CommentVO;
import com.itheima.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;
    @Autowired
    private CommentReportDao commentReportDao;

    /**
     * 添加评论
     *
     * @param commentVO
     *  @return void
     *@author itheima
     */
    public int add(CommentVO commentVO) {
        Comment comment = new Comment();
        comment.setBlogUid(commentVO.getBlogUid());
        comment.setContent(commentVO.getContent());
        comment.setSource(commentVO.getSource());
        comment.setStatus(String.valueOf(commentVO.getStatus()));
        comment.setToUid(commentVO.getToUid());
        comment.setToUserUid(commentVO.getToUserUid());
        comment.setTYPE(String.valueOf(commentVO.getType()));
        comment.setUid(commentVO.getUid());
        comment.setUserUid(commentVO.getUserUid());

        int count = commentDao.insertSelective(comment);
        return count;
    }

    /**
     * 删除评论
     * @param commentVO
     *@author itheima
     */
    @Override
    public void delete(CommentVO commentVO) {
        Comment comment = new Comment();
        if (commentVO.getUid() != null && !"".equals(commentVO.getUid())) {
            comment.setUid(commentVO.getUid());
            int count = commentDao.deleteByPrimaryKey(comment.getUid());
            if (count <= 0){
                throw new RuntimeException(MessageConstant.COMMENT_DELETE_FAIL);
            }
        } else {
            throw new RuntimeException(MessageConstant.COMMENT_DELETE_FAIL);
        }
    }

    /**
     * 删除选中评论
     *@param
     * @return void
     *@author itheima
     */
    @Override
    public void deleteBatch(List<CommentVO> commentVOList) {
        Comment comment = new Comment();
        if (commentVOList != null || commentVOList.size() > 0) {
            for (CommentVO commentVO : commentVOList) {
                if (commentVO.getUid() != null || "".equals(commentVO.getUid())) {
                    comment.setUid(commentVO.getUid());
                }
                commentDao.deleteByPrimaryKey(comment.getUid());
            }
        }
    }

    /**
     * 编辑评论
     *@param commentVO
     * @return void
     *@author itheima
     */
    @Override
    public void edit(CommentVO commentVO) {
        Comment comment = new Comment();
        comment.setBlogUid(commentVO.getBlogUid());
        comment.setContent(commentVO.getContent());
        comment.setSource(commentVO.getSource());
        comment.setStatus(String.valueOf(commentVO.getStatus()));
        comment.setToUid(commentVO.getToUid());
        comment.setToUserUid(commentVO.getToUserUid());
        comment.setTYPE(String.valueOf(commentVO.getType()));
        if (commentVO.getUid() != null || "".equals(commentVO.getUid())) {
            comment.setUid(commentVO.getUid());
        }
        comment.setUserUid(commentVO.getUserUid());

        commentDao.updateByPrimaryKeySelective(comment);
    }

    /**
     * 获取评论列表
     *@param commentVO
     * @return void
     *@author itheima
     */
    @Override
    public List<Comment> getList(CommentVO commentVO) {
        Comment comment = new Comment();
        Example example = new Example(comment.getClass());
        List<Comment> commentList = commentDao.selectByExample(example);
        if (commentList != null || commentList.size() > 0) {
            return commentList;
        } else {
            throw new RuntimeException(MessageConstant.COMMENT_FINDALL_FAIL);
        }
    }

    /**
     * 关闭评论邮件通知
     * @param userUid
     **@author itheima
     * @return
     */
    @Override
    public Boolean closeEmailNotification(String userUid) {
        Boolean flag = commentDao.closeEmailNotification(userUid);
        return flag;
    }

    /**
     * 获取评论列表和回复
     *@param userVO
     * @return java.util.List<com.itheima.pojo.User>
     *@author itheima
     */
    @Override
    public List<User> getListByUser(UserVO userVO) {
        User user = new User();
        user.setAvatar(userVO.getAvatar());
        user.setBirthday(userVO.getBirthday());
        user.setCommentStatus(userVO.getCommentStatus());
        commentDao.getListByUser();
        return null;
    }

    /**
     * 举报评论
     *
     * @param commentVO
     *  @return void
     *@author itheima
     */
    @Override
    public int report(CommentVO commentVO) {
        CommentReport commentReport = new CommentReport();
        commentReport.setContent(commentVO.getContent());
        commentReport.setStatus(String.valueOf(commentVO.getStatus()));
        commentReport.setReportCommentUid(commentVO.getToUid());
        commentReport.setReportUserUid(commentVO.getToUserUid());
        commentReport.setUid(commentVO.getUid());
        commentReport.setUserUid(commentVO.getUserUid());

        int count = commentReportDao.insertSelective(commentReport);
        return count;
    }

    /**
     * 获取用户点赞信息
     *@param
     * @return void
     *@author itheima
     */
    @Override
    public void getPraiseListByUser() {
        commentDao.getPraiseListByUser();
    }

    @Override
    public Integer getCommentCount() {
        Comment comment = new Comment();
        comment.setStatus("1");
        return commentDao.selectCount(comment);
    }
}
